package com.usernet.product.server;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MarketService extends HttpServlet {

	private UserService userService;
	private SoftwareService softwareService;
	private SearchService searchService;
	private PhotoService photoService;
	private ErrorService errorService;
	private DownloadService downloadService;
	private CommentService commentService;

	private static final long serialVersionUID = 1L;

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		userService = new UserService();
		softwareService = new SoftwareService();
		searchService = new SearchService();
		photoService = new PhotoService();
		errorService = new ErrorService();
		downloadService = new DownloadService();
		commentService = new CommentService();
	}

	@Override
	public void destroy() {
		super.destroy();
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter writer = response.getWriter();

		String ret = null;
		if (request.getParameter("type") == null) {
			ret = MarketUtils.getErrorJson("无此请求类型[type=空]");
		} else {
			int type = Integer.parseInt(request.getParameter("type"));

			switch (type) {
			case 1:
				// 用户登录[type=1]
				ret = userService.login(request);
				break;
			case 2:
				// 用户注册[type=2]
				ret = userService.reg(request);
				break;
			case 3:
				// 精品推荐[type=3]
				ret = softwareService.initNew(request);
				break;
			case 4:
				// 专题信息[type=4] // cache
				ret = softwareService.initResources(request);
				break;
			case 5:
				// 软件排名[type=5]
				ret = softwareService.initSort(request);
				break;
			case 6:
				// 分类信息[type=6] // cache
				ret = softwareService.initType(request);
				break;
			case 7:
				// 分类信息-详细软件列表[type=7]
				ret = softwareService.initTypeInfo(request);
				break;
			case 8:
				// 软件详细信息[type=8]
				ret = softwareService.getSoftware(request);
				break;
			case 9:
				// 搜索[type=9]
				ret = searchService.search(request);
				break;
			case 10:
				// 获取软件截图[type=10]
				ret = photoService.initPhoto(request);
				break;
			case 11:
				// 错误报告[type=11]
				ret = errorService.logcat(request);
				break;
			case 12:
				// 评分，评论[type=12]
				ret = errorService.comment(request);
				break;
			case 13:
				// 专题信息-详细软件列表[type=13]
				ret = softwareService.initResourcesInfo(request);
				break;
			case 14:
				// 下载软件[type=14]
				ret = downloadService.download(request);
				break;
			case 15:
				// 所有评论[type=15]
				ret = commentService.getAllComment(request);
				break;
			default:
				ret = MarketUtils.getErrorJson("无此请求类型[type=" + type + "]");
				break;
			}
		}
		writer.print(ret);
		writer.flush();
	}

}
